package com.imstlife.vending.utils;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import com.imstlife.vending.R;

/**
 * 类描述：自定义弹框（密码框）
 * 创建人：
 * 创建时间：
 */

public class DialogPassword extends Dialog implements View.OnClickListener {
    private static DialogPassword dialogPassword;
    private TextView psw_check;
    private Button one_btn, two_btn, three_btn, four_btn,
            five_btn, six_btn, seven_btn, eight_btn, nine_btn,
            zero_btn, delete_btn, ok_btn;
    private Context diacontext;
    private int flag;

    public DialogPassword(Context context, int flag) {
        super(context);
        this.diacontext = context;
        this.flag = flag;
        setCustomDialog();
    }

    public String getInputVal() {
        return psw_check.getText().toString();
    }

    //清空输入文本框
    public String cleanText() {
        psw_check.setText("");
        return psw_check.getText().toString();
    }

    public DialogPassword setOkListener(View.OnClickListener listener) {
        ok_btn.setOnClickListener(listener);
        return dialogPassword;
    }

    //单例模式 因为需要更改hint值 所以注了
    //    public static DialogPassword getInstance(Context context,int flag) {
    //
    //        if (dialogPassword == null) {
    //            synchronized (DialogPassword.class) {
    //                if (dialogPassword == null) {
    //                    dialogPassword = new DialogPassword(context,flag);
    //                }
    //            }
    //        }
    //        return dialogPassword;
    //    }
    private void setCustomDialog() {
        View view = LayoutInflater.from(diacontext).inflate(R.layout.dialog_password_keyboard, null);
        psw_check = (TextView) view.findViewById(R.id.psw_checkout);
        one_btn = (Button) view.findViewById(R.id.one);
        two_btn = (Button) view.findViewById(R.id.two);
        three_btn = (Button) view.findViewById(R.id.three);
        four_btn = (Button) view.findViewById(R.id.four);
        five_btn = (Button) view.findViewById(R.id.five);
        six_btn = (Button) view.findViewById(R.id.six);
        seven_btn = (Button) view.findViewById(R.id.seven);
        eight_btn = (Button) view.findViewById(R.id.eight);
        nine_btn = (Button) view.findViewById(R.id.nine);
        zero_btn = (Button) view.findViewById(R.id.zero);
        delete_btn = (Button) view.findViewById(R.id.delete);
        ok_btn = (Button) view.findViewById(R.id.ok);
        one_btn.setOnClickListener(this);
        two_btn.setOnClickListener(this);
        three_btn.setOnClickListener(this);
        four_btn.setOnClickListener(this);
        five_btn.setOnClickListener(this);
        six_btn.setOnClickListener(this);
        seven_btn.setOnClickListener(this);
        eight_btn.setOnClickListener(this);
        nine_btn.setOnClickListener(this);
        zero_btn.setOnClickListener(this);
        delete_btn.setOnClickListener(this);
        ok_btn.setOnClickListener(this);
        psw_check.setOnClickListener(this);

        if (flag == 0) {
            psw_check.setHint("请输入密码");
        } else if (flag == 1) {
            psw_check.setHint("请输入验证码");
        }
        super.requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.setContentView(view);
        Window window = this.getWindow();
        window.setGravity(Gravity.CENTER);
        //window.setWindowAnimations(R.style.mystyle);
        WindowManager.LayoutParams params = window.getAttributes();
        params.width = WindowManager.LayoutParams.WRAP_CONTENT;
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(params);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.one:
                psw_check.append("1");
                break;
            case R.id.two:
                psw_check.append("2");
                break;
            case R.id.three:
                psw_check.append("3");
                break;
            case R.id.four:
                psw_check.append("4");
                break;
            case R.id.five:
                psw_check.append("5");
                break;
            case R.id.six:
                psw_check.append("6");
                break;
            case R.id.seven:
                psw_check.append("7");
                break;
            case R.id.eight:
                psw_check.append("8");
                break;
            case R.id.nine:
                psw_check.append("9");
                break;
            case R.id.zero:
                psw_check.append("0");
                break;

            case R.id.delete:
                String str = getInputVal();
                if (str.equals("")) {
                    psw_check.setText("");
                } else {
                    char[] strarr = str.toCharArray();
                    String newstr = str.substring(0, strarr.length - 1);
                    psw_check.setText(newstr);
                }
                break;
            case R.id.ok:
                break;
            case R.id.psw_checkout:
                break;
        }
    }

    @Override
    public void show() {
        //保存弹框是否显示
        SharePersistentUtil.saveSharePersistent(diacontext, "dlgpws", "isnotdlgpsw", true);
        super.show();
    }

    @Override
    public void dismiss() {
        //保存弹框是否显示
        SharePersistentUtil.saveSharePersistent(diacontext, "dlgpws", "isnotdlgpsw", false);
        super.dismiss();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
    }

    @Override
    protected void onStop() {
        super.onStop();
        cleanText();
    }
}
